//
//  TSBasicItemModel.swift
//  TSLiveWallpaper
//
//  Created by 100Years on 2024/12/20.
//

open class TSBasicItemModel {
    // MARK: - Properties
    public var index: Int = 0
//    var clickBlock: ((Any?) -> Void)?
    public var tapBlock: ((TSBasicItemModel, Int, Any?) -> Void)?
    public var updateDataBlock: ((TSBasicItemModel) -> Void)?
    
    public var leftImageName: String?
    public var leftImage: UIImage?
    public var leftImagePath: String?
    public var leftTitle: String?
    public var leftSubTitle: String?
    public var rightString: String?
    public  var rightSubString: String?
    public var rightIntValue: Int = 0
    public var itemData: Any?
    public var identifier: String?
    public var parametView: UIView?
    public var rightIsHave: Bool = false
    public var bottomIsHave: Bool = false
    public var isSelected: Bool = false
    public var isWhether: Bool = false
    public var rightViewStyle: Int = 0
    public var rectCorner: UIRectCorner = []
    public var topSpace: Bool = false
    public var bottomSpace: Bool = false
    public var height: CGFloat = 0.0
    public var width: CGFloat = 0.0
    public var type: Int = 0
    public weak var attributionSection: TSBasicSectionModel?
    
    public init() {
        
    }
    
    // MARK: - Methods
    public func setCornerView(_ view: UIView, corner: CGFloat) {
        if rectCorner.rawValue == 0 || rectCorner.isEmpty {
            view.layer.cornerRadius = 0
        } else {
            let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: rectCorner, cornerRadii: CGSize(width: corner, height: corner))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            view.layer.mask = mask
        }
    }
    
    public func updateData() {
        updateDataBlock?(self)
    }
    
    
    public static func createItemModel(leftTitle: String,
                                rightViewStyle: Int,
                                rightString: String,
                                rightIsHave: Bool,
                                height: CGFloat,
                                rectCorner: UIRectCorner,
                                tapBlock: @escaping ((TSBasicItemModel, Int, Any?) -> Void)) -> TSBasicItemModel {
        let model = TSBasicItemModel()
        model.leftTitle = leftTitle
        model.rightViewStyle = rightViewStyle
        model.rightString = rightString
        model.rightIsHave = rightIsHave
        model.height = height
        model.rectCorner = rectCorner
        model.tapBlock = tapBlock
        return model
    }
    
    
    public static func setEdge(with dataArray: [TSBasicItemModel]) {
        guard !dataArray.isEmpty else { return }
        if dataArray.count == 1 {
            dataArray.first?.rectCorner = .allCorners
        } else {
            dataArray.first?.rectCorner = [.topLeft, .topRight]
            dataArray.last?.rectCorner = [.bottomLeft, .bottomRight]
        }
    }
}



open class TSBasicSectionModel {
    // MARK: - Properties
    open var index: Int = 0
    open var sectionData: Any?
    open var leftTitle: String?
    open var leftSubTitle: String?
    open var rightString: String?
    open var rightSubString: String?
    open var rightIsHave: Bool = false
    open var bottomIsHave: Bool = false
    open var topIsHave: Bool = false
    open var isOpen: Bool = false
    open var type: Int = 0
    open var rectCorner: UIRectCorner = []
    open var height: CGFloat = 0.0
    open var isFooter: Bool = false
    open var itemsArray: [TSBasicItemModel] = []
    open var selectedItemsIndex: Int = 0
    
    public init() {
        
    }
    
    // MARK: - Methods
    public func setCornerView(_ view: UIView, corner: CGFloat) {
        if rectCorner.rawValue == 0 || rectCorner.isEmpty {
            view.layer.cornerRadius = 0
        } else {
            let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: rectCorner, cornerRadii: CGSize(width: corner, height: corner))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            view.layer.mask = mask
        }
    }
    
    public func addSubItemModel(_ itemModel: TSBasicItemModel) {
        itemModel.attributionSection = self
        itemsArray.append(itemModel)
    }
    
    public static func createSectionModel(leftTitle: String, height: CGFloat, index: Int = 0) -> TSBasicSectionModel {
        let model = TSBasicSectionModel()
        model.leftTitle = leftTitle
        model.height = height
        model.index = index
        return model
    }
    
    public static func setSectionRectCorner(with dataArray: [TSBasicSectionModel]) {
        for section in dataArray {
            let itemsArray = section.itemsArray
            guard !itemsArray.isEmpty else { continue }
            if itemsArray.count == 1 {
                itemsArray.first?.rectCorner = .allCorners
            } else {
                itemsArray.first?.rectCorner = [.topLeft, .topRight]
                itemsArray.last?.rectCorner = [.bottomLeft, .bottomRight]
            }
        }
    }
}
